//////////////////////////////////////////////////////////////////////////
// FileName:ucPhraseDropDownList
// CreateBy:DragonXiong
// CreateDate:2010-05-20
// Description:
//		ucPhraseDropDownList usercontrol,used for all phrase by setting 
//      
// Modify Log:
//            
//           
//	
// Copyright:ASL
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Auto.Data.Common;
using Auto.Web.Common;


namespace Auto.BusMan.UserControl
{

    public partial class ucPhraseListBox : System.Web.UI.UserControl
    {
        #region --Property--
        /// <summary>
        /// Phrase Class(PhraseID)
        /// </summary>
        //public Phrase PhraseClass
        //{
        //    get { return (ViewState["PhraseClass"] == null) ? Phrase.PMDBType : (Phrase)ViewState["PhraseClass"]; }
        //    set { ViewState["PhraseClass"] = value; }
        //}

        public string PhraseClass
        {
            get { return (ViewState["PhraseClass"] == null) ? "" : (string)ViewState["PhraseClass"]; }
            set { ViewState["PhraseClass"] = value; }
        }

        //public bool HasValue
        //{
        //    get { return (ViewState["HasValue"] == null) ? false : Convert.ToBoolean(ViewState["HasValue"]); }
        //    set { ViewState["HasValue"] = value; }
        //}

        /// <summary>
        /// DropDownList
        /// </summary>
        public ListBox ListBox
        {
            get { return lbPhrase; }
        }

        /// <summary>
        /// Width of DropDownList
        /// </summary>
        public Unit Width
        {
            set { lbPhrase.Width = value; }
        }

        /// <summary>
        /// Enable
        /// </summary>
        public bool Enabled
        {
            get { return (ViewState["Enabled"] == null) ? false : (Boolean)ViewState["Enabled"]; }
            set
            {
                ViewState["Enabled"] = value;
                lbPhrase.Enabled = value;
            }
        }

        /// <summary>
        /// SelectedValue of ListBox
        /// </summary>
        public string SelectedValue
        {
            get
            {
                string strSelectItem = "";
                for (int i = 0; i <= lbPhrase.Items.Count - 1; i++)
                {

                    if (lbPhrase.Items[i].Selected)
                    {
                        if (strSelectItem != "")
                        {
                            strSelectItem += "," + lbPhrase.Items[i].Value;// +"'";
                        }
                        else
                        {
                            strSelectItem = lbPhrase.Items[i].Value;// "'" + +"'";
                        }
                    }
                }
                return strSelectItem;
            }

            set
            {
                int i;
                for (i = 0; i < lbPhrase.Items.Count; i++)
                {
                    string phrase = lbPhrase.Items[i].Value;
                    foreach (string xphrase in value.Split(','))
                    {
                        if (xphrase == phrase)
                        {
                            lbPhrase.Items[i].Selected = true;
                        }
                    }

                }
            }

        }

        /// <summary>
        /// SelectedText of ListBox
        /// </summary>
        public string SelectedText
        {
            get
            {
                string strSelectItem = "";
                for (int i = 0; i <= lbPhrase.Items.Count - 1; i++)
                {

                    if (lbPhrase.Items[i].Selected)
                    {
                        if (strSelectItem != "")
                        {
                            strSelectItem += "," + lbPhrase.Items[i].Text;// +"'";
                        }
                        else
                        {
                            strSelectItem =  lbPhrase.Items[i].Text;// "'" ++ "'";
                        }
                    }
                }
                return strSelectItem;
            }
        }
        #endregion --Property--

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack /*&& HasValue*/)
            //{
            //    InitControl();
            //}
        }

        /// <summary>
        /// Fill the Data to the Control
        /// </summary>
        public void InitControl()
        {
            IList<SysPhrase.SysPhraseDetail> phraseDetail = new SysPhrase(SessionBox.CurrentSystem, PhraseClass.ToString()).ListDetails;
            BindlbPhrase(phraseDetail);
            foreach (SysPhrase.SysPhraseDetail model in phraseDetail)
            {
                if (model.IsDefault)
                {
                    SelectedValue = model.PhraseValue;
                    break;
                }
            }
        }

        /// <summary>
        /// BindData to Control
        /// </summary>
        /// <param name="phraseDetail">ILIST<SysPhrase.SysPhraseDetail></param>
        private void BindlbPhrase(IList<SysPhrase.SysPhraseDetail> phraseDetail)
        {

            lbPhrase.DataSource = phraseDetail;
            lbPhrase.DataTextField = SysPhrase.SysPhraseDetail.FIELD_PhraseText;
            lbPhrase.DataValueField = SysPhrase.SysPhraseDetail.FIELD_PhraseValue;
            lbPhrase.DataBind();
        }

        /// <summary>
        /// Clear The Selection of ListBox
        /// </summary>
        public void ClearSelectItem()
        {
            this.lbPhrase.ClearSelection();
        }
    }
}